<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row>
                <el-alert
                    title="添加商品信息"
                    type="info"
                    center
                    show-icon>
                </el-alert>
             </el-row>
             <!-- 进度条 -->
             <el-row>
                 <el-steps class="addsteps" align-center :space="200" :active="activeSteps-0" finish-status="success">
                    <el-step title="基本信息">
                    </el-step>
                    <el-step title="商品参数"></el-step>
                    <el-step title="商品属性"></el-step>
                    <el-step title="商品图片"></el-step>
                    <el-step title="商品内容"></el-step>
                    <el-step title="完成"></el-step>
                </el-steps>
             </el-row>
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRrf" label-width="100px" >
                <el-tabs :tab-position="'left'" :before-leave="beforeTagLeave" v-model="activeSteps" :tab-click="tabClick">
                    <el-tab-pane name='0' label="基本信息">
                        <el-form-item label="商品名称" prop="goods_name">
                            <el-input v-model="addForm.goods_name"></el-input>
                        </el-form-item>
                        <el-form-item label="商品价格" prop="goods_price">
                            <el-input type="number" v-model="addForm.goods_price"></el-input>
                        </el-form-item>
                        <el-form-item label="商品数量" prop="goods_number">
                            <el-input type="number" v-model="addForm.goods_number"></el-input>
                        </el-form-item>
                        <el-form-item label="商品重量" prop="goods_weight">
                            <el-input type="number" v-model="addForm.goods_weight"></el-input>
                        </el-form-item>
                        <el-form-item label="商品分类" >
                            <el-cascader
                                v-model="selectID"
                                :options="goodsCate"
                                :props=props
                                @change="handleChange">
                            </el-cascader>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane name='1' label="商品参数">用户管理</el-tab-pane>
                    <el-tab-pane name='2' label="商品属性">配置管理</el-tab-pane>
                    <el-tab-pane name='3' label="商品图片">角色管理</el-tab-pane>
                    <el-tab-pane name='5' label="商品内容">定时任务补偿</el-tab-pane>
                </el-tabs>
            </el-form>
        </el-card>
    </div>
</template>

<script>
export default {
  data () {
    return {

      // 表单对象
      addForm: {
        goods_name: '',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0
      },
      // 表单规则
      addFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ]
      },
      //   级联选择器选中的id
      selectID: [],
      props: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
      //   商品分类数据
      goodsCate: [],
      //   进度条
      activeSteps: '0'
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$Message.error('请求商品分类失败')
      }
      this.goodsCate = res.data
    },
    // 级联选择器发送变化时
    handleChange () {
      if (this.selectID.length !== 3) {
        this.selectID = []
      }
    },
    beforeTagLeave (activeName, oldActiveName) {
      if (this.selectID.length !== 3 && oldActiveName === '0') {
        this.$Message.error('请选择分类')
        return false
      }
    },
    // tap选中的时候触发
    async tabClick () {
      console.log(11)
      if (this.activeSteps === '1') {
        const { data: res } = await this.$http.get(`goods/${this.threeId}`)
        if (res.meta.status !== 200) {
          return this.$Message.error('请求商品分类失败')
        }
        console.log(res)
      }
    }
  },
  computed: {
    threeId () {
      if (this.selectID === 3) {
        return this.selectID[2]
      }
      return null
    }
  }
}
</script>

<style lang="less" scoped>
.addsteps{
    font-size: 13px;
    margin: 15px;
}
</style>
